﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillShipmentService
    {
        /// <summary>
        /// 查询出货单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="shipmentno"></param>
        /// <returns></returns>
        public BillShipment GetBillShipment(long id, String shipmentno)
        {
            BillShipment shipment = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    shipment = new BillShipment();
                    shipment.Id = id;
                    shipment.Shipmentno = shipmentno;
                    shipment = new BillShipmentDAO().GetBillShipment(conn, shipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return shipment;
        }

        /// <summary>
        /// 查询出货单列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo">企业号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="memberno">客户编号</param>
        /// <param name="adminUsername">出货人</param>
        /// <param name="startdate">出货开始日期</param>
        /// <param name="enddate">出货结束日期</param>
        /// <param name="debtamount">欠款金额</param>
        /// <param name="ascfield"></param>
        /// <param name="descfield"></param>
        /// <returns></returns>
        public DataTable GetBillShipmentTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String shipmentno, String memberno,
            String adminUsername, String startdate, String enddate, Decimal debtamount, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipment shipment = new BillShipment();
                    shipment.Pageindex = pageindex;
                    shipment.Pagesize = pagesize;
                    shipment.EnterpriseNo = enterpriseNo;
                    shipment.StoreNo = storeNo;
                    shipment.Shipmentno = shipmentno;
                    shipment.Memberno = memberno;
                    shipment.AdminUsername = adminUsername;
                    shipment.StartDate = startdate;
                    shipment.EndDate = enddate;
                    shipment.DebtAmount = debtamount;
                    shipment.Ascfield = ascfield;
                    shipment.Descfield = descfield;
                    dt = new BillShipmentDAO().GetBillShipmentTable(conn, shipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询出货单记录数
        /// </summary>
        /// <param name="enterpriseNo">企业号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="memberno">客户编号</param>
        /// <param name="adminUsername">出货人</param>
        /// <param name="startdate">出货开始日期</param>
        /// <param name="enddate">出货结束日期</param>
        /// <param name="debtAmount">欠款金额</param>
        /// <returns></returns>
        public long GetBillShipmentRecord(String enterpriseNo, String storeNo, String shipmentno, String memberno,
            String adminUsername, String startdate, String enddate, Decimal debtAmount)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillShipment shipment = new BillShipment();
                    shipment.EnterpriseNo = enterpriseNo;
                    shipment.StoreNo = storeNo;
                    shipment.Shipmentno = shipmentno;
                    shipment.Memberno = memberno;
                    shipment.AdminUsername = adminUsername;
                    shipment.StartDate = startdate;
                    shipment.EndDate = enddate;
                    shipment.DebtAmount = debtAmount;
                    record = new BillShipmentDAO().GetBillShipmentRecord(conn, shipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 插入出货单
        /// </summary>
        /// <param name="shipment"></param>
        /// <param name="list"></param>
        /// <param name="memberMoney"></param>
        /// <returns></returns>
        public long InsertBillShipment(BillShipment shipment, ArrayList list, MemberMoney memberMoney)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillShipmentDAO().InsertBillShipment(conn, shipment);
                    if (id > 0)
                    {
                        //保存商品信息
                        new BillShipmentGoodsService().InsertBillShipmentGoods(conn, list);
                        //会员是否存在
                        if (!String.IsNullOrEmpty(shipment.Memberno))
                        {
                            //扣除会员卡内余额
                            if (shipment.MemberBalance > 0 && memberMoney != null)
                            {
                                new MemberMoneyService().InsertMemberMoney(conn, memberMoney);
                            }

                            int historyIntegral = 0, lessIntegral = 0, addIntegral = 0;
                            MemberIntegralService memberIntegralService = new MemberIntegralService();
                            //扣除支付使用的积分
                            if (!String.IsNullOrEmpty(shipment.Memberno) && shipment.PayIntegral > 0)
                            {
                                lessIntegral = shipment.PayIntegral;
                                MemberIntegral memberIntegral = new MemberIntegral();
                                memberIntegral.EnterpriseNo = shipment.EnterpriseNo;
                                memberIntegral.StoreNo = shipment.StoreNo;
                                memberIntegral.MemberNo = shipment.Memberno;
                                memberIntegral.Type = (int)IntegralEnum.OUT;
                                memberIntegral.Integral = shipment.PayIntegral;
                                memberIntegral.TransNo = shipment.Shipmentno;
                                memberIntegral.Description = "积分支付";
                                memberIntegral.TransTime = DateTime.Now;
                                memberIntegral.AdminUsername = shipment.AdminUsername;
                                memberIntegral.AdminName = shipment.AdminName;
                                memberIntegral.IsSuccess = true;
                                memberIntegral.CreateTime = DateTime.Now;
                                memberIntegralService.InsertMemberIntegral(conn, memberIntegral);
                            }
                            //保存会员积分
                            if (!String.IsNullOrEmpty(shipment.Memberno) && Decimal.Add(shipment.PayAmount, shipment.MemberBalance) > 0)
                            {
                                historyIntegral = (int)Decimal.Add(shipment.PayableAmount, shipment.MemberBalance);
                                addIntegral = historyIntegral;
                                MemberIntegral memberIntegral = new MemberIntegral();
                                memberIntegral.EnterpriseNo = shipment.EnterpriseNo;
                                memberIntegral.StoreNo = shipment.StoreNo;
                                memberIntegral.MemberNo = shipment.Memberno;
                                memberIntegral.Type = (int)IntegralEnum.IN;
                                memberIntegral.Integral = historyIntegral;
                                memberIntegral.TransNo = shipment.Shipmentno;
                                memberIntegral.Description = "POS销售";
                                memberIntegral.TransTime = DateTime.Now;
                                memberIntegral.AdminUsername = shipment.AdminUsername;
                                memberIntegral.AdminName = shipment.AdminName;
                                memberIntegral.IsSuccess = true;
                                memberIntegral.CreateTime = DateTime.Now;
                                memberIntegralService.InsertMemberIntegral(conn, memberIntegral);
                            }
                            int saveIntegral = addIntegral - lessIntegral;
                            if (saveIntegral >= 0)
                            {
                                new MemberService().UpdateMemberIntegral(conn, shipment.Memberno, historyIntegral, saveIntegral, IntegralEnum.IN);
                            }
                            else
                            {
                                new MemberService().UpdateMemberIntegral(conn, shipment.Memberno, historyIntegral, Math.Abs(saveIntegral), IntegralEnum.OUT);
                            }
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新出货单
        /// </summary>
        /// <param name="shipment"></param>
        /// <returns></returns>
        public int UpdateBillShipment(BillShipment shipment)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new BillShipmentDAO().UpdateBillshipment(conn, shipment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除出货单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteBillShipment(String shipmentnos)
        {
            int count = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    if (!String.IsNullOrEmpty(shipmentnos))
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        String[] arr = shipmentnos.Split(new Char[] { ',' });
                        BillShipmentDAO shipmentDAO = new BillShipmentDAO();
                        BillShipment shipment = new BillShipment();
                        BillShipmentGoodsService shipmentGoodsService = new BillShipmentGoodsService();
                        foreach (String lopShipmentno in arr)
                        {
                            shipment.Shipmentno = lopShipmentno;
                            int flag = shipmentDAO.DeleteBillShipment(conn, shipment);
                            if (flag > 0)
                            {
                                shipmentGoodsService.DeleteBillshipmentGoods(conn, lopShipmentno);
                                count++;
                            }
                        }
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }      
            return count;
        }

        /// <summary>
        /// 更新出货单欠款余额
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="shipmentno"></param>
        /// <param name="debtAmount"></param>
        /// <returns></returns>
        public int UpdateDebtAmount(DbConnection conn, String shipmentno, decimal debtAmount)
        {
            int flag = 0;
            try
            {
                BillShipment shipment = new BillShipment();
                shipment.Shipmentno = shipmentno;
                shipment.DebtAmount = debtAmount;
                flag = new BillShipmentDAO().UpdateDebtAmount(conn, shipment);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
